package com.wangzy.game;

/**
 * @ProjectName: SportBS
 * @Date: 2022/8/19
 * @Desc:
 */
public class DirectorThread extends Thread {

    private IGameCanvas gameCanvas;
    private volatile boolean pauseFlag=false;

    public DirectorThread(IGameCanvas gameCanvas) {
        this.gameCanvas = gameCanvas;
    }

    public void pause(){
        pauseFlag=true;
    }

    public void unPuase(){
        pauseFlag=false;
    }

    public boolean isPause(){
        return pauseFlag;
    }

    @Override
    public void run() {

        while (!interrupted()) {
            if(!pauseFlag){
                if (null != gameCanvas) {
                    gameCanvas.refresh();
                    int delay = 1000 / gameCanvas.getFps();
                    delay(delay);
                }else{
                    delay(1000/30);
                }
            }else{
                delay(1000/30);
            }
        }
    }

    public void delay(long time){
        try {
            Thread.sleep(time);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    public void finish() {
        this.interrupt();
    }
}